package com.frogs.ucenter.v1.service.platform.center;

import java.util.Map;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.frogs.ucenter.v1.service.PlatformOperationService;
import com.frogs.ucenter.v1.util.ApiConstants;

@Service(ApiConstants.VERSION+"platformOperationCenter")
public class PlatformOperationCenter {

	private Map<Integer, PlatformOperationService> platformOperationServiceMap;

	public Map<Integer, PlatformOperationService> getPlatformOperationServiceMap() {
		return platformOperationServiceMap;
	}

	public void setPlatformOperationServiceMap(
			Map<Integer, PlatformOperationService> platformOperationServiceMap) {
		this.platformOperationServiceMap = platformOperationServiceMap;
	}
	
	/**
	 * 获取openId
	 * 
	 * @param object
	 * @return
	 */
	public JSONObject getOpenId(JSONObject object) {
        Integer openPlatformId = object.getIntValue("openPlatformId");
        PlatformOperationService platform = this.getService(openPlatformId);
        return platform.getOpenId(object);
    }
	
	/**
	 * 刷新token
	 * 
	 * @param object
	 * @return
	 */
	public JSONObject getAccessTokenByRefreshToken(JSONObject object) {
		Integer openPlatformId = object.getIntValue("openPlatformId");
        PlatformOperationService platform = this.getService(openPlatformId);
        return platform.getAccessTokenByRefreshToken(object);
    }
	
	/**
	 * 是否存在openId
	 * 
	 * @param object
	 * @return
	 */
	public Boolean isExistsOpenid(JSONObject object) {
		Integer openPlatformId = object.getIntValue("openPlatformId");
        PlatformOperationService platform = this.getService(openPlatformId);
        return platform.isExistsOpenid(object);
	}
	
	/**
	 * 更新token
	 * 
	 * @param object
	 * @return
	 */
	public Boolean updateTokenInfo(JSONObject object) {
		Integer openPlatformId = object.getIntValue("openPlatformId");
        PlatformOperationService platform = this.getService(openPlatformId);
        return platform.updateTokenInfo(object);
	}
	
	/**
	 * 获取昵称
	 * 
	 * @param object
	 * @return
	 */
	public JSONObject getNickName(JSONObject object) {
		Integer openPlatformId = object.getIntValue("openPlatformId");
        PlatformOperationService platform = this.getService(openPlatformId);
        return platform.getNickName(object);
	}
	
	/**
	 * 注册
	 * 
	 * @param object
	 * @return
	 * @throws Exception 
	 */
	public JSONObject register(JSONObject object) throws Exception {
		Integer openPlatformId = object.getIntValue("openPlatformId");
        PlatformOperationService platform = this.getService(openPlatformId);
        return platform.register(object);
	}
	
	private PlatformOperationService getService(Integer openPlatformId){
		PlatformOperationService operate = platformOperationServiceMap.get(openPlatformId);
		return operate;
	}
}
